Count the number of each character in text fileΒΆ
Count the number of each character of a given text
of a text file.
import collections
import pprint
file_input = input('File Name: ')
with open(file_input, 'r') as f:
count = collections.Counter(f.read().upper())
value = pprint.pformat(count)
print(value)
Output:
File Name: abc.txt
Counter({' ': 93,
'E': 64,
'N': 45,
'A': 42,
'T': 40,
'I': 36,
'O': 31,
'R': 29,
'H': 25,
'D': 19,
'M': 17,
'Y': 17,
'L': 15,
'F': 15,
'U': 14,
'C': 13,
'G': 13,
'S': 12,
',': 7,
'B': 6,
'W': 5,
'9': 5,
'.': 4,
'P': 4,
'1': 3,
'\n': 2,
'0': 2,
'3': 2,
':': 1,
'-': 1,
'K': 1,
'(': 1,
')': 1,
'V': 1})